package com.pearadmin.pro.modules.laboratoryManagement.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.laboratoryManagement.domain.Dangerousroom;
import com.pearadmin.pro.modules.laboratoryManagement.domain.DangerurroomRequest;
import com.pearadmin.pro.modules.laboratoryManagement.service.LMService;
import com.pearadmin.pro.modules.sys.domain.SysDept;
import com.pearadmin.pro.modules.sys.domain.SysDictData;
import com.pearadmin.pro.modules.sys.domain.SysRole;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import com.pearadmin.pro.modules.sys.service.SysDeptService;
import com.pearadmin.pro.modules.sys.service.SysDictDataService;
import com.pearadmin.pro.modules.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实训室危险品管理
 *
 * @author ZhouXingYun
 * @date 2022/5/18 11:09
 */
@Api(tags = {"实训室危险品管理"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "lm")
public class LMController extends BaseController {

    @Resource
    private LMService lmService;
    @Resource // 数据字典业务类
    private SysDictDataService sysDictDataService;

    @Resource // 角色 业务类
    private SysRoleService sysRoleService;


    @Resource // 用户基本信息
    private UserContext userContext;

    /**
     * 查询实训室危险物品列表
     *
     * @param request 查询参数
     */
    @GetMapping("page")
    @Log(title = "查询实训室危险物品列表")
    @ApiOperation(value = "查询实训室危险物品列表")
    public Result page(DangerurroomRequest request) {
        PageResponse<Dangerousroom> page = lmService.page(request);
        // 返回值处理方法 , 将匹配数据字典
        List<Dangerousroom> record = page.getRecord();
        page.setRecord(returnValueProcessingMethod(record));
        return success(page);
    }

    /**
     * 从数据字典获取模块
     *
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/4/20 15:32
     */
    @GetMapping("getLMModule")
    @Log(title = "获取模块")
    @ApiOperation(value = "获取模块")
    public Result getModule() {
        // 类型
        List<SysDictData> typeList = sysDictDataService.selectModule("danger_type");
        // 单位
        List<SysDictData> unitList = sysDictDataService.selectModule("danger_unit");
        // 部门
        List<SysRole> sysDeptList = sysRoleService.list();

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("typeList", typeList);
        returnMap.put("unitList", unitList);
        returnMap.put("sysDeptList", sysDeptList);
        return success(returnMap);
    }


    /**
     * 新增库存
     *
     * @param dangerousroom
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/18 16:31
     */
    @PostMapping("save")
    @Log(title = "新增库存")
    @ApiOperation(value = "新增库存")
    public Result save(@RequestBody Dangerousroom dangerousroom) {
        dangerousroom.setNumber(dangerousroom.getGoodssum());
        dangerousroom.setCreatetime(new Date());
        dangerousroom.setCreatename(userContext.getUserId());
        dangerousroom.setUpdatetime(new Date());
        dangerousroom.setUpdatename(userContext.getUserId());

        return auto(lmService.saveStock(dangerousroom));
    }




    /**
     * 修改危险物品库存
     * @author ZhouXingYun
     * @date 2022/5/19 10:11
     * @param dangerousroom 危险物品库存实体
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @PutMapping("edit")
    @Log(title = "修改危险物品库存")
    @ApiOperation(value = "修改危险物品库存")
    public Result edit(@RequestBody Dangerousroom dangerousroom) {
        return auto(lmService.updateById( dangerousroom));
    }


    /**
     * 删除库存
     * @author ZhouXingYun
     * @date 2022/5/19 10:21
     * @param id 库存编号
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @DeleteMapping("remove")
    @Log(title = "删除危险物品库存")
    @ApiOperation(value = "删除危险物品库存")
    public Result remove(@RequestParam String id) {
        return auto(lmService.removeById(id));
    }

    /**
     * 批量删除危险物品库存
     *
     * @param ids 用户编号
     */
    @DeleteMapping("removeBatch")
    @Log(title = "批量删除危险物品库存")
    @ApiOperation(value = "批量删除危险物品库存")
    public Result removeBatch(@RequestParam List<String> ids) {
        return auto(lmService.removeByIds(ids));
    }


    /**
     * 返回值处理方法
     *
     * @param list
     * @return java.lang.Object
     * @author ZhouXingYun
     * @date 2022/5/18 14:30
     */
    public List<Dangerousroom> returnValueProcessingMethod(List<Dangerousroom> list) {
        List<SysDictData> typeList = sysDictDataService.selectModule("danger_type");
        List<SysDictData> unitList = sysDictDataService.selectModule("danger_unit");
        for (Dangerousroom dangerousroom : list) {
            String type = dangerousroom.getType(); // 物品类型
            String unit = dangerousroom.getUnit(); // 计量单位
            for (SysDictData sysDictData : typeList) {
                String code = sysDictData.getValue();
                if (code.equals(type)) {
                    dangerousroom.setType(sysDictData.getLabel());
                }
            }
            for (SysDictData sysDictData : unitList) {
                String code = sysDictData.getValue();
                if (code.equals(unit)) {
                    dangerousroom.setUnit(sysDictData.getLabel());

                }
            }
        }
        return list;
    }


//    /**
//     * 清空日志
//     *
//     * @param isAuth 日志类型
//     */
//    @DeleteMapping("clean")
//    @Log(title = "清空日志")
//    @ApiOperation(value = "清空日志")
//    public Result clean(Boolean isAuth){
//        if(isAuth) {
//            return auto(sysLogService.lambdaUpdate()
//                    .eq(SysLog::getAction, Action.AUTH)
//                    .remove());
//        } else {
//            return auto(sysLogService.lambdaUpdate()
//                    .ne(SysLog::getAction, Action.AUTH)
//                    .remove());
//        }
//    }
//
//    @GetMapping("/export")
//    @Log(title = "导出日志")
//    @ApiOperation(value = "导出日志")
//    @Excel(clazz = SysLog.class, model = Model.WRITE)
//    public List<SysLog> export() {
//        return sysLogService.list(new SysLogRequest());
//    }
}
